<?

class Upload
{
    var $directory_name;
    var $max_filesize;
    var $error;
    var $user_tmp_name;
    var $user_file_name;
    var $user_file_size;
    var $user_file_type;
    var $user_full_name;
    var $thumb_name;
	var $resized = false;

    function Upload($file)
	{
		$this->setTmpName($file['tmp_name']);
		$this->setFileSize($file['size']);
		$this->setFileType($file['type']);
		$this->setFileName($file['name']);
	}

	function setDirectory($dir_name = ".")
    {
        $this->directory_name = $dir_name;
    }

    function setMaxSize($max_file = 300000)
    {
        $this->max_filesize = $max_file;
    }

    function error()
    {
        return $this->error;
    }

    function isOk()
    {
        if(isset($this->error))
            return FALSE;
        else
            return TRUE;
    }

    function setTmpName($temp_name)
    {
        $this->user_tmp_name = $temp_name;
    }

    function setFileSize($file_size)
    {
        $this->user_file_size = $file_size;
    }

    function setFileType($file_type)
    {
        $this->user_file_type = $file_type;
    }

    function setFileName($file)
    {
        $this->user_file_name = $file;
        $this->user_full_name = $this->directory_name."/".$this->user_file_name;
    }

    function resize($max_width = 0, $max_height = 0 )
    {
		if(preg_match("/png$/",$this->user_file_type))
		{
            $img = imagecreatefrompng($this->user_full_name);
		}

		if(preg_match("/(jpg|jpeg)$/",$this->user_file_type))
		{
            $img = imagecreatefromjpeg($this->user_full_name);
		}

		if(preg_match("/gif$/",$this->user_file_type))
		{
            $img = imagecreatefromgif($this->user_full_name);
		}

        $FullImage_width = imagesx($img);
        $FullImage_height = imagesy($img);

        if(isset($max_width) && isset($max_height) && $max_width > 0 && $max_height > 0)
        {
            $new_width = $max_width;
            $new_height = $max_height;
        }
        else if(isset($max_width) && $max_width > 0)
        {
            $new_width = $max_width;
            $new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);
        }
        else if(isset($max_height) && $max_height > 0)
        {
            $new_height = $max_height;
            $new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);
        }
        else
        {
            $new_height = $FullImage_height;
            $new_width = $FullImage_width;
        }

        $full_id =  imagecreatetruecolor( $new_width , $new_height );
        imagecopyresampled( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );


        if(preg_match("/\.(jpg|jpeg)$/",$this->user_full_name))
        {
            $full = imagejpeg( $full_id, $this->user_full_name,100);
        }

        if(preg_match("/\.png$/",$this->user_full_name))
        {
            $full = imagepng( $full_id, $this->user_full_name);
        }

        if(preg_match("/\.gif$/",$this->user_full_name))
        {
            $full = imagegif($full_id, $this->user_full_name);
        }
        imagedestroy( $full_id );
        unset($max_width);
        unset($max_height);
		$this->resized = true;
    }

    function startCopy()
    {
        if(!isset($this->user_file_name))
            $this->error = "You must define filename!";

        if ($this->user_file_size <= 0)
            $this->error = "File size error (0): $this->user_file_size KB<br>";

        if (!isset($this->error))
        {
            $filename = basename($this->user_file_name);

            if (!empty($this->directory_name))
                $destination = $this->user_full_name;
            else
                $destination = $filename;

            if(!is_uploaded_file($this->user_tmp_name))
                $this->error = "File ".$this->user_tmp_name." is not uploaded correctly.";

            if (!move_uploaded_file($this->user_tmp_name,$destination))
            {
				$this->error = "Impossible to copy ".$this->user_file_name." from $userfile to destination directory.";
        	}
		}
    }

    function setThumbnailName($thumbname)
    {
        if(preg_match("/\.png$/",$this->user_full_name))
            $this->thumb_name = $this->directory_name."/".$thumbname.".png";
        if(preg_match("/\.(jpg|jpeg)$/",$this->user_full_name))
            $this->thumb_name = $this->directory_name."/".$thumbname.".jpg";
        if(preg_match("/\.gif$/",$this->user_full_name))
            $this->thumb_name = $this->directory_name."/".$thumbname.".gif";
    }

    function createThumbnail()
    {
        if (!copy($this->user_full_name, $this->thumb_name))
        {
            echo "<br>".$this->user_full_name.", ".$this->thumb_name."<br>";
            echo "failed to copy $file...<br />\n";
        }
    }

    function setThumbnailSize($max_width = 0, $max_height = 0 )
    {
		if($this->resized == false)
		{
			$orig_name = $this->user_file_type;
		}
		else
		{
			$orig_name = $this->user_full_name;
		}

		if(preg_match("/png$/",$orig_name))
		{
            $img = imagecreatefrompng($this->thumb_name);
		}

		if(preg_match("/(jpg|jpeg)$/",$orig_name))
		{
            $img = imagecreatefromjpeg($this->thumb_name);
		}

		if(preg_match("/gif$/",$orig_name))
		{
            $img = imagecreatefromgif($this->thumb_name);
		}

        $FullImage_width = imagesx ($img);
        $FullImage_height = imagesy ($img);

        if(isset($max_width) && isset($max_height) && $max_width != 0 && $max_height != 0)
        {
            $new_width = $max_width;
            $new_height = $max_height;
        }
        else if(isset($max_width) && $max_width != 0)
        {
            $new_width = $max_width;
            $new_height = ((int)($new_width * $FullImage_height) / $FullImage_width);
        }
        else if(isset($max_height) && $max_height != 0)
        {
            $new_height = $max_height;
            $new_width = ((int)($new_height * $FullImage_width) / $FullImage_height);
        }
        else
        {
            $new_height = $FullImage_height;
            $new_width = $FullImage_width;
        }

        $full_id = imagecreatetruecolor( $new_width , $new_height );
        imagecopyresampled( $full_id, $img, 0,0,0,0, $new_width, $new_height, $FullImage_width, $FullImage_height );

        if(preg_match("/\.(jpg|jpeg)$/",$this->thumb_name))
        {
            $full = imagejpeg( $full_id, $this->thumb_name,100);
        }

        if(preg_match("/\.png$/",$this->thumb_name))
        {
            $full = imagepng( $full_id, $this->thumb_name);
        }

        if(preg_match("/\.gif$/",$this->thumb_name))
        {
            $full = imagegif($full_id, $this->thumb_name);
        }
        imagedestroy( $full_id );
        unset($max_width);
        unset($max_height);
    }

}
?>
